import { _decorator, Component, instantiate, Node } from 'cc';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { GAME_PREFAB } from '../../Data/GlobalData';
import { ClearPallet } from './ClearPallet';
import { Tools } from '../../Tool/Tools';
import { TouchPallet } from './TouchPallet';
const { ccclass, property } = _decorator;

@ccclass('ClearLoad')
/**
 * 消失层
 */
export class ClearLoad extends BaseComponent {

    //盘子个数
    private PalletNum: number = 24;

    //盘子数组
    private PalletArr: Node[] = [];

    protected Init(): void {
        for (let i = 0; i < this.PalletNum; i++) {
            let cp: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.ClearPallet]);
            this.node.addChild(cp);
            this.PalletArr.push(cp);
        }
    }

    protected OnShow(): void {
        for (let cp of this.PalletArr) {
            cp.getComponent(ClearPallet).Init();
        }
    }

    /**
     * 判断是否碰到了
     */
    public CheckHit(_touchpallet: Node): boolean {
        for (let cp of this.PalletArr) {
            if (Tools.isIntersect(_touchpallet, cp, 100, 60)) {
                cp.getComponent(ClearPallet).IamShow();
                for (let fd of _touchpallet.parent.getComponent(TouchPallet).FoodIndex) {
                    if (fd != -1) cp.getComponent(ClearPallet).AddFoode(fd)
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 判断合成
     */
    private CheckSynthesis() {
        for (let i = 0; i < this.node.children.length; i++) {
            if (this.node.children[i].getComponent(ClearPallet).ReturnFirstFood() != -1) {//如果我身上有食物我就去判断

                //判断我得右侧和我下侧是否有和我一样得食物,然后再让相同得再和周边判断，把一样得
                let hor_index: number = i % 4;
                let ver_index: number = Math.floor(i / 4);

                if (hor_index < 3) {//判断右侧

                }

                if (ver_index < 5) {//判断下方

                }

            }
        }
    }


}


